//
//  ZGSendAddressCell.m
//  NewBee
//
//  Created by leergou on 16/8/22.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "ZGSendAddressCell.h"
#import "ZGSendAddressModel.h"

#define kMargin 5

@interface ZGSendAddressCell ()

// nameLabel
@property (nonatomic,weak) UILabel *nameLabel;
// telNumLabel
@property (nonatomic, weak) UILabel *telNumLabel;
// addressLabel
@property (nonatomic, weak) UILabel *addressLabel;
// lineView
@property (nonatomic, weak) UIView *lineView;
// editButton
@property (nonatomic, weak) UIButton *editButton;


@end


@implementation ZGSendAddressCell
#pragma mark - 构造方法 -
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 设置界面
        [self setupUI];
    }
    return self;
}

#pragma mark - 按钮点击 -
- (void)editButtonClick:(UIButton *)sender{
    
    if (self.buttonClick) {
        self.buttonClick();
    }
    
}

#pragma mark - setupUI -
- (void)setupUI{
    
    self.selectionStyle  = UITableViewCellSelectionStyleNone;
    
    // 1.添加子控件
    UILabel *nameLabel = [[UILabel alloc] init];
    self.nameLabel = nameLabel;
    nameLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.nameLabel];
    
    
    UILabel *telNumLabel = [[UILabel alloc] init];
    self.telNumLabel = telNumLabel;
    telNumLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.telNumLabel];
    
    
    UILabel *addressLabel = [[UILabel alloc] init];
    self.addressLabel = addressLabel;
    addressLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.addressLabel];
    
    
    UIView *lineView = [[UIView alloc] init];
    self.lineView = lineView;
    lineView.backgroundColor = kGlobalGray;
    [self.contentView addSubview:self.lineView];
    
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editButton = editButton;
    [editButton setImage:[UIImage imageNamed:@"v2_address_edit_highlighted"] forState:UIControlStateNormal];
    editButton.adjustsImageWhenHighlighted = NO;
    self.editButton = editButton;
    [self.contentView addSubview:self.editButton];
    
    [editButton addTarget:self action:@selector(editButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 2.添加约束
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(kMargin);
        make.width.equalTo(65);
    }];
    
    [self.telNumLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nameLabel.trailing);
        make.centerY.equalTo(self.nameLabel);
    }];
    
    [self.addressLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(kMargin);
        make.bottom.equalTo(-kMargin);
    }];
    
    [self.editButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.centerX.equalTo(self.lineView).offset(25);
    }];
    
    [self.lineView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(1);
        make.top.equalTo(kMargin);
        make.bottom.equalTo(-kMargin);
        make.trailing.equalTo(-50);
    }];
}


#pragma mark - 重写模型 set 方法 -
- (void)setDetailAddressModel:(ZGSendAddressModel *)detailAddressModel{
    _detailAddressModel = detailAddressModel;
    self.nameLabel.text = detailAddressModel.name;
    self.telNumLabel.text = detailAddressModel.telNum;
    self.addressLabel.text = [NSString stringWithFormat:@"%@%@%@",detailAddressModel.city,detailAddressModel.zone,detailAddressModel.detailAddr];
    
    //    [self.nameLabel sizeToFit];
    [self.telNumLabel sizeToFit];
    [self.addressLabel sizeToFit];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
